package cn.sunlight.wordread.utils;

import cn.sunlight.wordread.pojo.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ReadDoc
 * @Description: T读取word数据工具类
 * @Author: sunlight
 * @Date: 9:42 - 2022/3/7
 * @Version: 1.0
 **/
public class ReadDoc {

    private static final Logger log= LoggerFactory.getLogger(ReadDoc.class);

    public void ReadByDoc(String path) throws Exception {
        InputStream is = new FileInputStream(path);
        HWPFDocument doc = new HWPFDocument(is);
        // 输出书签信息
        //this.bookmarkInfo(doc.getBookmarks());

        //读表格
        Range range = doc.getRange();
        this.readTable(range);

        is.close();
    }

    /**
     * 读表格
     * 每一个回车符代表一个段落，所以对于表格而言，每一个单元格至少包含一个段落，每行结束都是一个段落。
     * @param range
     */
    private void readTable(Range range) throws Exception {
        //遍历range范围内的table。
        TableIterator tableIter = new TableIterator(range);
        Table table;
        TableRow row;
        TableCell cell;
        Resume resume;
        //雪花算法生成唯一id
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        //日期转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //教育经历集合
        List<Education> edus=new ArrayList<>();
        //工作经验集合
        List<WorkExperience> workList=new ArrayList<>();
        //项目经验
        List<ProjectExperience> projectList=new ArrayList<>();
        //技能证书
        List<SkillCertificate> skillList=new ArrayList<>();
        while (tableIter.hasNext()) {
            table = tableIter.next();
            int rowNum = table.numRows();
            resume = new Resume();
            resume.setId(idWorker.nextId());
            WorkExperience work = new WorkExperience();
            WorkExperience work1 = new WorkExperience();
            WorkExperience work2 = new WorkExperience();
            WorkExperience work3 = new WorkExperience();
            WorkExperience work4 = new WorkExperience();
            ProjectExperience project=new ProjectExperience();
            ProjectExperience project1=new ProjectExperience();
            ProjectExperience project2=new ProjectExperience();
            ProjectExperience project3=new ProjectExperience();
            ProjectExperience project4=new ProjectExperience();

            for (int i = 0; i < rowNum; i++) {
                switch (i){
                    case 1:
                        row=table.getRow(i);
                        String name = row.getCell(1).text().trim();
                        resume.setName(name);
                        String sex = row.getCell(3).text().trim();
                        resume.setSex(sex);
                        String age = row.getCell(5).text().trim();
                        resume.setAge(age);
                        String maritalStatus = row.getCell(7).text().trim();
                        resume.setMaritalStatus(maritalStatus);
                        break;
                    case 2:
                        row=table.getRow(i);
                        String phoneTel = row.getCell(1).text().trim();
                        resume.setPhoneTel(phoneTel);
                        String wechatNum = row.getCell(3).text().trim();
                        resume.setWechatNum(wechatNum);
                        String email = row.getCell(5).text().trim();
                        resume.setEmail(email);
                        String dateBirth = row.getCell(7).text().trim();

                        resume.setDateBirth(DateUtils.parseDate(dateBirth));
                        break;
                    case 3:
                        row=table.getRow(i);
                        String birthAddress = row.getCell(1).text().trim();
                        resume.setBirthAddress(birthAddress);
                        break;
                    case 4:
                        row=table.getRow(i);
                        String residentialAddress = row.getCell(1).text().trim();
                        resume.setResidentialAddress(residentialAddress);
                        break;
                    case 6:
                    case 7:
                    case 8:
                        row=table.getRow(i);
                        Education education=new Education();
                        education.setEduId(idWorker.nextId());
                        education.setResumeId(resume.getId());

                        String educationDate = row.getCell(1).text().trim();
                        String[] educationDateSplit = educationDate.split("-");
                        education.setStartTime(DateUtils.parseDate(educationDateSplit[0]));
                        education.setEndTime(DateUtils.parseDate(educationDateSplit[1]));
                        String school = row.getCell(3).text().trim();
                        education.setSchool(school);
                        String major = row.getCell(5).text().trim();
                        education.setMajor(major);
                        String certificate = row.getCell(7).text().trim();
                        education.setCertificate(certificate);
                        edus.add(education);
                        resume.setEducation(edus);
                        break;
                    case 10:
                        row=table.getRow(i);
                        work.setWorkId(idWorker.nextId());
                        work.setResumeId(resume.getId());
                        String startStopWork = row.getCell(1).text().trim();
                        String[] startStopWorkSplit = startStopWork.split("-");
                        work.setStartTime(DateUtils.parseDate(startStopWorkSplit[0]));
                        work.setEndTime(DateUtils.parseDate(startStopWorkSplit[1]));
                        String compName = row.getCell(3).text().trim();
                        work.setCompName(compName);
                        String position = row.getCell(5).text().trim();
                        work.setPosition(position);
                        break;
                    case 11:
                        row=table.getRow(i);
                        String wd = row.getCell(1).text().trim();
                        work.setWorkDesc(wd);
                        workList.add(work);
                        resume.setWorkExperiences(workList);
                        break;
                    case 12:
                        row=table.getRow(i);
                        work1.setWorkId(idWorker.nextId());
                        work1.setResumeId(resume.getId());
                        String startStopWork1 = row.getCell(1).text().trim();
                        String[] startStopWorkSplit1 = startStopWork1.split("-");
                        work1.setStartTime(DateUtils.parseDate(startStopWorkSplit1[0]));
                        work1.setEndTime(DateUtils.parseDate(startStopWorkSplit1[1]));
                        String compName1 = row.getCell(3).text().trim();
                        work1.setCompName(compName1);
                        String position1 = row.getCell(5).text().trim();
                        work1.setPosition(position1);
                        break;
                    case 13:
                        row=table.getRow(i);
                        String wd1 = row.getCell(1).text().trim();
                        work1.setWorkDesc(wd1);
                        workList.add(work1);
                        resume.setWorkExperiences(workList);
                        break;
                    case 14:
                        row=table.getRow(i);
                        work2.setWorkId(idWorker.nextId());
                        work2.setResumeId(resume.getId());
                        String startStopWork2 = row.getCell(1).text().trim();
                        String[] startStopWorkSplit2 = startStopWork2.split("-");
                        work2.setStartTime(DateUtils.parseDate(startStopWorkSplit2[0]));
                        work2.setEndTime(DateUtils.parseDate(startStopWorkSplit2[1]));
                        String compName2 = row.getCell(3).text().trim();
                        work2.setCompName(compName2);
                        String position2 = row.getCell(5).text().trim();
                        work2.setPosition(position2);
                        break;
                    case 15:
                        row=table.getRow(i);
                        String wd2 = row.getCell(1).text().trim();
                        work2.setWorkDesc(wd2);
                        workList.add(work2);
                        resume.setWorkExperiences(workList);
                        break;
                    case 16:
                        row=table.getRow(i);
                        work3.setWorkId(idWorker.nextId());
                        work3.setResumeId(resume.getId());
                        String startStopWork3 = row.getCell(1).text().trim();
                        String[] startStopWorkSplit3 = startStopWork3.split("-");
                        work3.setStartTime(DateUtils.parseDate(startStopWorkSplit3[0]));
                        work3.setEndTime(DateUtils.parseDate(startStopWorkSplit3[1]));
                        String compName3 = row.getCell(3).text().trim();
                        work3.setCompName(compName3);
                        String position3 = row.getCell(5).text().trim();
                        work3.setPosition(position3);
                        break;
                    case 17:
                        row=table.getRow(i);
                        String wd3 = row.getCell(1).text().trim();
                        work3.setWorkDesc(wd3);
                        workList.add(work3);
                        resume.setWorkExperiences(workList);
                        break;
                    case 18:
                        row=table.getRow(i);
                        work4.setWorkId(idWorker.nextId());
                        work4.setResumeId(resume.getId());
                        String startStopWork4 = row.getCell(1).text().trim();
                        String[] startStopWorkSplit4 = startStopWork4.split("-");
                        work4.setStartTime(DateUtils.parseDate(startStopWorkSplit4[0]));
                        work4.setEndTime(DateUtils.parseDate(startStopWorkSplit4[1]));
                        String compName4 = row.getCell(3).text().trim();
                        work4.setCompName(compName4);
                        String position4 = row.getCell(5).text().trim();
                        work4.setPosition(position4);
                        break;
                    case 19:
                        row=table.getRow(i);
                        String wd4 = row.getCell(1).text().trim();
                        work4.setWorkDesc(wd4);
                        workList.add(work4);
                        resume.setWorkExperiences(workList);
                        break;
                    case 20:
                        row=table.getRow(i);
                        row.getCell(0).text().trim();
                        break;
                    case 21:
                        row=table.getRow(i);
                        project.setProjectId(idWorker.nextId());
                        project.setResumeId(resume.getId());
                        String projectDate = row.getCell(1).text().trim();
                        String[] projectDateSplit = projectDate.split("-");
                        project.setStartTime(DateUtils.parseDate(projectDateSplit[0]));
                        project.setEndTime(DateUtils.parseDate(projectDateSplit[1]));
                        String projectName = row.getCell(3).text().trim();
                        project.setProjectName(projectName);
                        String holdPosition = row.getCell(5).text().trim();
                        project.setHoldPosition(holdPosition);

                        break;
                    case 22:
                        row=table.getRow(i);
                        String projectDesc = row.getCell(1).text().trim();
                        project.setProjectDesc(projectDesc);
                        break;
                    case 23:
                        row=table.getRow(i);
                        String responsibilities = row.getCell(1).text().trim();
                        project.setResponsibilities(responsibilities);
                        break;
                    case 24:
                        row=table.getRow(i);
                        String performance = row.getCell(1).text().trim();
                        project.setPerformance(performance);

                        projectList.add(project);
                        resume.setProjectExperiences(projectList);
                        break;
                    case 25:
                        row=table.getRow(i);
                        project1.setProjectId(idWorker.nextId());
                        project1.setResumeId(resume.getId());
                        String projectDate1 = row.getCell(1).text().trim();
                        String[] projectDateSplit1 = projectDate1.split("-");
                        project1.setStartTime(DateUtils.parseDate(projectDateSplit1[0]));
                        project1.setEndTime(DateUtils.parseDate(projectDateSplit1[1]));
                        String projectName1 = row.getCell(3).text().trim();
                        project1.setProjectName(projectName1);
                        String holdPosition1 = row.getCell(5).text().trim();
                        project1.setHoldPosition(holdPosition1);
                        break;
                    case 26:
                        row=table.getRow(i);
                        String projectDesc1 = row.getCell(1).text().trim();
                        project1.setProjectDesc(projectDesc1);
                        break;
                    case 27:
                        row=table.getRow(i);
                        String responsibilities1 = row.getCell(1).text().trim();
                        project1.setResponsibilities(responsibilities1);
                        break;
                    case 28:
                        row=table.getRow(i);
                        String performance1 = row.getCell(1).text().trim();
                        project1.setPerformance(performance1);

                        projectList.add(project1);
                        resume.setProjectExperiences(projectList);
                        break;
                    case 29:
                        row=table.getRow(i);
                        project2.setProjectId(idWorker.nextId());
                        project2.setResumeId(resume.getId());
                        String projectDate2 = row.getCell(1).text().trim();
                        String[] projectDateSplit2 = projectDate2.split("-");
                        project2.setStartTime(DateUtils.parseDate(projectDateSplit2[0]));
                        project2.setEndTime(DateUtils.parseDate(projectDateSplit2[1]));
                        String projectName2 = row.getCell(3).text().trim();
                        project2.setProjectName(projectName2);
                        String holdPosition2 = row.getCell(5).text().trim();
                        project2.setHoldPosition(holdPosition2);
                        break;
                    case 30:
                        row=table.getRow(i);
                        String projectDesc2 = row.getCell(1).text().trim();
                        project2.setProjectDesc(projectDesc2);
                        break;
                    case 31:
                        row=table.getRow(i);
                        String responsibilities2 = row.getCell(1).text().trim();
                        project2.setResponsibilities(responsibilities2);
                        break;
                    case 32:
                        row=table.getRow(i);
                        String performance2 = row.getCell(1).text().trim();
                        project2.setPerformance(performance2);

                        projectList.add(project2);
                        resume.setProjectExperiences(projectList);
                        break;
                    case 33:
                        row=table.getRow(i);
                        project3.setProjectId(idWorker.nextId());
                        project3.setResumeId(resume.getId());
                        String projectDate3 = row.getCell(1).text().trim();
                        String[] projectDateSplit3 = projectDate3.split("-");
                        project3.setStartTime(DateUtils.parseDate(projectDateSplit3[0]));
                        project3.setEndTime(DateUtils.parseDate(projectDateSplit3[1]));
                        String projectName3 = row.getCell(3).text().trim();
                        project3.setProjectName(projectName3);
                        String holdPosition3 = row.getCell(5).text().trim();
                        project3.setHoldPosition(holdPosition3);
                        break;
                    case 34:
                        row=table.getRow(i);
                        String projectDesc3 = row.getCell(1).text().trim();
                        project3.setProjectDesc(projectDesc3);
                        break;
                    case 35:
                        row=table.getRow(i);
                        String responsibilities3 = row.getCell(1).text().trim();
                        project3.setResponsibilities(responsibilities3);
                        break;
                    case 36:
                        row=table.getRow(i);
                        String performance3 = row.getCell(1).text().trim();
                        project3.setPerformance(performance3);

                        projectList.add(project3);
                        resume.setProjectExperiences(projectList);
                        break;
                    case 37:
                        row=table.getRow(i);
                        project4.setProjectId(idWorker.nextId());
                        project4.setResumeId(resume.getId());
                        String projectDate4 = row.getCell(1).text().trim();
                        String[] projectDateSplit4 = projectDate4.split("-");
                        project4.setStartTime(DateUtils.parseDate(projectDateSplit4[0]));
                        project4.setEndTime(DateUtils.parseDate(projectDateSplit4[1]));
                        String projectName4 = row.getCell(3).text().trim();
                        project4.setProjectName(projectName4);
                        String holdPosition4 = row.getCell(5).text().trim();
                        project4.setHoldPosition(holdPosition4);
                        break;
                    case 38:
                        row=table.getRow(i);
                        String projectDesc4 = row.getCell(1).text().trim();
                        project4.setProjectDesc(projectDesc4);
                        break;
                    case 39:
                        row=table.getRow(i);
                        String responsibilities4 = row.getCell(1).text().trim();
                        project4.setResponsibilities(responsibilities4);
                        break;
                    case 40:
                        row=table.getRow(i);
                        String performance4 = row.getCell(1).text().trim();
                        project4.setPerformance(performance4);
                        projectList.add(project4);
                        resume.setProjectExperiences(projectList);
                        break;
                    case 42:
                    case 43:
                    case 44:
                    case 45:
                    case 46:
                    case 47:
                    case 48:
                    case 49:
                    case 50:
                    case 51:
                        row=table.getRow(i);
                        SkillCertificate skill = new SkillCertificate();
                        skill.setSkillId(idWorker.nextId());
                        skill.setResumeId(resume.getId());
                        String skillType = row.getCell(1).text().trim();
                        skill.setSkillType(skillType);
                        String skillName = row.getCell(3).text().trim();
                        skill.setSkillName(skillName);
                        String proficiency = row.getCell(5).text().trim();
                        skill.setProficiency(proficiency);

                        skillList.add(skill);
                        resume.setSkillCertificates(skillList);

                        break;
                    case 53:
                        row=table.getRow(i);
                        String evaluation = row.getCell(0).text().trim();
                        resume.setEvaluation(evaluation);
                        break;
                    case 55:
                        row=table.getRow(i);
                        String currentStatus = row.getCell(1).text().trim();
                        resume.setCurrentStatus(currentStatus);
                        String salaryExpectation = row.getCell(3).text().trim();
                        resume.setSalaryExpectation(salaryExpectation);
                        String expectedPosition = row.getCell(5).text().trim();
                        resume.setExpectedPosition(expectedPosition);
                        break;
                    case 57:
                        row=table.getRow(i);
                        String quitReason = row.getCell(0).text().trim();
                        resume.setQuitReason(quitReason);
                        break;
                    default:
                        break;
                }
            }
            format.format(resume.getDateBirth());
            System.out.println("基本信息："+resume);
        }
    }

    /**
     * 输出书签信息
     * @param bookmarks
     */
    private void bookmarkInfo(Bookmarks bookmarks) {

        int count = bookmarks.getBookmarksCount();
        System.out.println("书签数量：" + count);
        Bookmark bookmark;
        for (int i=0; i<count; i++) {
            bookmark = bookmarks.getBookmark(i);
            System.out.println("书签" + (i+1) + "的名称是：" + bookmark.getName());
            System.out.println("开始位置：" + bookmark.getStart());
            System.out.println("结束位置：" + bookmark.getEnd());
        }

    }

}
